package google.android.ageofempires;

import java.lang.ref.Reference;
import java.sql.Ref;

import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.Point;
import android.graphics.Bitmap.Config;

/**
 * 
 * @author CaoMinhTri
 *
 */
public class Utility {
	
	/**
	 * Rotate bitmap
	 * @param orgImg
	 * @param x
	 * @param y
	 * @param degrees
	 * @return
	 */
	public Bitmap rotateBitmap(Bitmap orgImg, float x, float y, float degrees) {
		Matrix matrix = new Matrix();
		matrix.postRotate(degrees, x, y);
		
		Bitmap rotateImg = Bitmap.createBitmap(orgImg, 0, 0, orgImg.getWidth(), 
											   orgImg.getHeight(), matrix, false);
		
		return rotateImg;
	}
	
	/**
	 * Get degree between two point
	 * @param p1
	 * @param p2
	 * @return
	 */
	public double getDegree(Point p1, Point p2) {		
		int xDistance = p2.x - p1.x;
		int yDistance = p2.y - p1.y;
		double distance = Math.sqrt(Math.pow(xDistance, 2) + Math.pow(yDistance, 2));
		double sinAlpha = (double)(yDistance / distance);
		double alpha = Math.toDegrees(Math.asin(sinAlpha));
		if ((alpha > 0) && (xDistance > 0)) {
			alpha = alpha;
		} else if ((alpha > 0) && (xDistance < 0)) {
			alpha = 180 - alpha;
		} else if ((alpha < 0) && (xDistance > 0)) {
			alpha += 360;
		} else if ((alpha < 0) && (xDistance < 0)) {
			alpha += 270;
		} 
		return alpha;
	}
	
	/**
	 * Transparent bitmap
	 * @param seed
	 * @return
	 */
	public Bitmap transparency(Bitmap seed)
	{						
		int mWidth = seed.getWidth();
		int mHeight = seed.getHeight();
		int[] mArrayColor = new int[mWidth * mHeight];
		Bitmap bitmap = Bitmap.createBitmap(mWidth, mHeight, Config.ARGB_8888);				
		
		try	{
			/* get pixel left-top */
			int mColor = seed.getPixel(0, 0);
			
			/* get all pixel of image */
			seed.getPixels(mArrayColor, 0, mWidth, 0, 0, mWidth, mHeight);
			
//			for (int i = 0; i < mWidth; i++) {
//				for (int j = 0; j < mHeight; j++) {
//					
//					/* compare pixel */
//					if(mArrayColor[(i * mWidth) + j] == mColor) {					
//						mArrayColor[i] = 0;		// transparent						
////						seed.setPixel(i, j, Color.TRANSPARENT);	// transparent
////						transBit.setPixel(i, j, Color.TRANSPARENT);
//					}
//				}				
//			}
			for (int i = 0; i < mArrayColor.length; i++) {
				
				/* compare pixel */
				if(mArrayColor[i] == mColor) {					
					mArrayColor[i] = 0;		// transparent					
				}
			}					
			bitmap.setPixels(mArrayColor, 0, mWidth, 0, 0, mWidth, mHeight);
			seed = null;			
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		}							
		return bitmap;
	}
	
	/**
	 * Flip bitmap X-axis
	 * @param orgImg
	 * @return
	 */
	public static Bitmap flipBitmap(Bitmap orgImg) {
		Matrix matrix = new Matrix();
		matrix.preScale(-1.0f, 1.0f);
		
		Bitmap flipImg = Bitmap.createBitmap(orgImg, 0, 0, orgImg.getWidth(), 
											 orgImg.getHeight(), matrix, false);		
		return flipImg;
	}
}
